/**
 * 
 */
package controller.xml;

import java.io.File;
import java.io.IOException;

import model.cnf.CNF;
import model.cnf.FileCNF;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * Use to parse a {@link CNF} in the exported session in XML.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class CNFXMLHandler extends XMLHandler {

	private CNF				cnf			= null;
	private CNFXMLStatus	status		= CNFXMLStatus.NONE;
	private File			directory	= null;

	/**
	 * Create a {@link CNFXMLHandler} .
	 * 
	 * @param directory
	 *            where to find the {@link CNF}.
	 */
	public CNFXMLHandler(File directory) {
		this.directory = directory;
	}


	@Override
	public boolean characters2(char[] ch, int start, int length) throws SAXException {

		switch (status) {
		case NONE:
		case STAT:
			throw new SAXException("not supposed to receive character in this status");
		default:
			return super.characters2(ch, start, length);
		}
	}

	@Override
	public boolean endDocument2() throws SAXException {
		return false;
	}

	@Override
	public boolean endElement2(String uri, String localName, String qName) throws SAXException {
		switch (status) {
		case NONE:
			return false;

		case NAME:
			if (qName.equals(XMLBaliseFormat.CNF_NAME))
			{// ferme cnf
				setName();
				status = CNFXMLStatus.NONE;
				return true;
			}
			throw new SAXException("end of NAME was expected");
		case STAT:
			if (qName.equals(XMLBaliseFormat.CNF_STAT))
			{// ferme stat
				status = CNFXMLStatus.NONE;
				cnf.setStat(((StatXMLHandler) delegate).getStat());
				delegate = null;
				return true;
			}
			throw new SAXException("end of STAT was expected");

		}

		// attend une fin de balise qui existe pas...
		throw new SAXException("an other end of element was expected");
	}

	@Override
	public boolean startElement2(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
		if (status != CNFXMLStatus.NONE)
			throw new SAXException("already have a started element");

		if (arg2.equals(XMLBaliseFormat.CNF_NAME))
		{// balise name
			if (cnf != null)
				throw new SAXException("already have a CNF");

			// set les donnees pour recupere le name
			status = CNFXMLStatus.NAME;
			return true;
		}

		if (arg2.equals(XMLBaliseFormat.CNF_STAT))
		{// balise stat
			if (cnf == null)
				throw new SAXException("don't have a CNF");

			// set les donnees pour recupere les stat
			status = CNFXMLStatus.STAT;
			delegate = new StatXMLHandler();

			return true;
		}

		// peut pas commencer balise qui existe pas
		throw new SAXException("unexpected balise: " + arg2);
	}

	/**
	 * Return the cnf.
	 * 
	 * @return the cnf
	 */
	public CNF getCnf() {
		return cnf;
	}

	/**
	 * Set the {@link CNF}.
	 * 
	 * @throws SAXException
	 */
	private void setName() throws SAXException {
		String tmp = new String(buffer.toString());

		try
		{
			cnf = new FileCNF(new File(directory, tmp));
		} catch (IOException e)
		{
			e.printStackTrace();
			throw new SAXException("problem getting cnf");
		}
	}

}
